<?php

class Damages_Methods_Request_GetCarData extends Damages_Methods_Request {

  /**
   *
   * @var string
   */
  public $vin;

  public function __construct($params) {
    parent::__construct($params);
    $this->vin = $params->vin;
  }

  public function process() {
    $response = new Damages_Methods_Response_GetCarData(true);
    $data = $this->getDbObject();
    if ($data) {
      $response->setData($data);
    }
    return $response;
  }

  /**
   * 
   * @return DamagesCarData
   */
  private function getDbObject() {
    $q = DamagesCarDataQuery::create()
            ->filterByVin($this->vin)
            ->joinDealersRelatedByDealerId(null, Criteria::LEFT_JOIN);
    if (Damages_Auth::getInstance()->getUser()->getPermissions()->getCode() == Damages_Auth::PERMISSION_SD) {
      $dealers = new PropelObjectCollection();
      foreach (Damages_Auth::getInstance()->getUser()->getUserDealerss() as $userDealer) {
        $dealer = $userDealer->getDealers();
        $dealers->append($dealer);
        if ($dealer->getDealersRelatedByParentId())
          $dealers->append($dealer->getDealersRelatedByParentId());
      }
//      foreach (Damages_Auth::getInstance()->getUser()->getDealerss() as $dealer) {
//      }
      $q = $q->filterByDealersRelatedByDealerId($dealers)
              ->_or()
              ->filterByDealersRelatedByDealerParentId($dealers);
    }
    $q = $q->joinDictBrands()
            ->with('DealersRelatedByDealerId')
            ->with('DictBrands');
    $result = $q->find();

    return $result->isEmpty() ? null : $result->getFirst();
  }

}